# 构建阶段
FROM node:18 as builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# WASM构建阶段
FROM rust:1.70 as wasm-builder
WORKDIR /wasm
COPY wasm/Cargo.toml wasm/Cargo.lock ./
COPY wasm/src ./src
RUN cargo build --release --target wasm32-unknown-unknown

# 运行阶段
FROM node:18-slim
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=wasm-builder /wasm/target/wasm32-unknown-unknown/release/core_wasm.wasm ./dist/wasm/

EXPOSE 50051
HEALTHCHECK --interval=30s --timeout=3s \
  CMD curl -f http://localhost:50051/health || exit 1

CMD ["node", "dist/index.js"]